//
//  UIButton+Extensions.swift
//  扩展文件
//
//  Created by 伏文东 on 2018/2/5.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit

extension UIButton {
    /// 创建文本按钮
    ///
    /// - Parameters:
    ///   - title:               标题文字
    ///   - fontSize:            字体大小
    ///   - mormalColor:         默认颜色
    ///   - highlightedColor:    高亮颜色
    ///   - backgroundImageName: 背景图像名称
    /// - Returns:               返回按钮
    static func textButton(title: String, fontSize: CGFloat, normalColor: UIColor, highlightedColor: UIColor, backgroundImageName: String? = nil) -> UIButton{
        
        let button = UIButton()
        button.setTitle(title, for: .normal)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        button.setTitleColor(normalColor, for: .normal)
        button.setTitleColor(highlightedColor, for: .highlighted)
        
        if backgroundImageName != nil {
            button.setBackgroundImage(UIImage(named: backgroundImageName ?? ""), for: .normal)
            
            let backgroundImageNameHL = (backgroundImageName ?? "") + "_highlighted"
            button.setBackgroundImage(UIImage(named: backgroundImageNameHL), for: .highlighted)
        }
        
        button.sizeToFit()
        return button
    }
    
    /// 创建图像按钮
    ///
    /// - Parameters:
    ///   - imageName:              图像名称
    ///   - backgroundImageName:    背景图像名称
    /// - Returns:                  返回按钮
    static func imageButton(imageName: String, backgroundImageName: String) -> UIButton {
        let button = UIButton()
        
        button.setImage(UIImage(named: imageName), for: .normal)
        let imageNameHL = imageName + "_highlighted"
        button.setImage(UIImage(named: imageNameHL), for: .highlighted)
        
        button.setBackgroundImage(UIImage(named: backgroundImageName), for: .normal)
        let backgroundImageNameHL = backgroundImageName + "_highlighted"
        button.setBackgroundImage(UIImage(named: backgroundImageNameHL), for: .highlighted)
        
        button.sizeToFit()
        
        return button
    }

}
